﻿@{
    ViewBag.Title = "ApplyList";
    Layout = "~/Areas/IM/Views/Shared/_Layout.cshtml";
}

<script src="~/Scripts/pinyin.js"></script>

@section Scripts{
    <script type="text/javascript">
        var Materials;
        var boolSearch = true;
        var Doctor = 0;//登录人是否为医生20160302
        $(document).ready(function () {
            DisableBackspace();
            initSearch();
            bindGrid();
            Materials = new Array();
        });
        $(window).resize(function () {
            $.setGridWithSearchBar("#TableForm", "#SearchBar", "#gridSurplusList");
        });
        function bindGrid() {
            //2018-12-11 zch 

            @*var personRole = "@ViewBag.personRole";//获取登录人角色-20160302
            if (personRole == "Doctor" || personRole == "Nurse") {
                $('#MaterialType').combobox('setValue', "贵重药品");//分类选择
                $("#MaterialTypeInfo").val("MaterialType-15");
                $('#MaterialType').combobox('disable');//禁用--分类20160302
            }*@

            $("#gridSurplusList").datagrid({
                width: 'auto',
                height: 'auto',
                fitColumns: true,
                striped: true,
                singleSelect: false,
                selectOnCheck: true,
                checkOnSelect: true,
                url: '@Url.Content("~/IM/I_Apply/GetLastDaySurplusList")',
                loadMsg: '数据加载中请稍后……',
                pagination: true,//启用分页，默认每页10行
                rownumbers: true,//显示页码，默认 提供 10 - 50 的页容量选择下拉框
                pageSize: 15,//设置 页容量5为
                pageList: [10, 15, 20],//设置 页容量下拉框
                queryParams: {
                    SurplusCode: $("#Storage").combobox("getValue"),
                    MaterialType: $("#MaterialTypeInfo").val(),
                    MaterialID: $("#MaterialID").val()
                },
                frozenColumns: [
                    [
                   { field: 'ck', checkbox: true },
                   { title: '物资名称', field: 'MaterialName', width: '15%', sortable: false },
                   { title: '批次号', field: 'RealBatchNo', width: '10%', sortable: false, formatter: $.formatRealBatchNo },
                   { title: "规格", field: "MaterialSpecification", width: '5%', align: "center" },
                   { title: "单位", field: "MaterialUnitName", width: '5%', align: "center" },
                   { title: "库存数量", field: "Surplus", width: '8%', align: "center" },
                   { title: '有效期', field: 'ValidityDate', width: '10%', align: 'center', sortable: false, formatter: $.formatValidityDate },
                   { title: '仓库', field: 'StorageName', width: '13%', sortable: false },
                   { title: "生产厂家", field: "MaterialManufacturer", width: '10%', align: "center" },
                   { title: "供应商", field: "MaterialVendor", width: '10%', align: "center" },
                   { title: 'ID', field: 'ID', sortable: false, hidden: true },
                   { title: 'StorageCode', field: 'StorageCode', sortable: false, hidden: true },
                   { title: 'MaterialID', field: 'MaterialID', sortable: false, hidden: true }
                    ]
                ],
                rowStyler: function (index, row) { },
                toolbar: [
                    {
                        id: 'btnApplyView',
                        text: '申领数量共计[0]种',
                        iconCls: 'icon-custom-cGoods',
                        iconWidth: 22,
                        handler: function () {
                            if (Materials.length > 0) {
                                $('#ApplyDetailList').dialog('open');
                                initDetailGrid();
                            } else {
                                $.messager.alert('提示', '请选择物资', 'info', function () {
                                });
                            }
                        }
                    }
                ],
                onCheck: function (index, row) {
                    if (Materials.length == 0) {
                        Materials.push(row);
                        changeDeliveryViewNum();
                        return;
                    }
                    var boolContain = false;
                    $.each(Materials, function (i, info) {
                        if (info.ID == row.ID) {
                            boolContain = true;
                        }
                    });
                    if (!boolContain) {
                        Materials.push(row);
                        changeDeliveryViewNum();
                    }
                },
                onUncheck: function (index, row) {
                    var obj;
                    $.each(Materials, function (i, info) {
                        if (info.ID == row.ID) {
                            obj = info;
                        }
                    });
                    Materials.remove(obj);
                    changeDeliveryViewNum();
                },
                onLoadSuccess: function (data) {
                    //20151127--增加加载成功后，隐藏全选按钮
                    $("#gridSurplusList").parent().find("div .datagrid-header-check").children("input[type=\"checkbox\"]").eq(0).attr("style", "display:none;");

                    setDeliveryViewNumCheck();
                    changeDeliveryViewNum();
                }
            });
            $(".datagrid-header-check").attr("disabled", "disabled");
            $.setGridWithSearchBar("#TableForm", "#SearchBar", "#gridSurplusList");
        }
        function reSearch() {
            $("#gridSurplusList").datagrid({
                queryParams: {
                    SurplusCode: $("#Storage").combotree("getValue"),
                    MaterialType: $("#MaterialTypeInfo").val(),
                    MaterialID: $("#MaterialID").val()
                }
            });
        }
        function changeDeliveryViewNum() {
            if (Materials.length == 0) {
                $("#btnApplyView").linkbutton({
                    text: "申领数量共计[" + Materials.length + "]种",
                    iconCls: "icon-custom-cGoods"
                });
            } else if (Materials.length > 0) {
                $("#btnApplyView").linkbutton({
                    text: "申领数量共计[<strong><span style='color:red'>" + Materials.length + "</span></strong>]种",
                    iconCls: "icon-custom-cGoodsFull"
                });
            }
        }
        function setDeliveryViewNumCheck() {
            var rows = $('#gridSurplusList').datagrid('getRows');
            $('#gridSurplusList').datagrid('uncheckAll');
            $.each(rows, function (index, item) {
                $.each(Materials, function (i, info) {
                    if (item.ID == info.ID) {
                        $('#gridSurplusList').datagrid('checkRow', index);
                    }
                });
            });
        }
        function initSearch() {
            var urlstr1 = '@Url.Content("~/IM/AllDictionary/GetStorageCombo")';
            $.EUIcombobox("#Storage", {
                url: urlstr1,
                valueField: 'ID',
                textField: 'Name',
                editable: false,
                panelHeight: 'auto',
                datainfo: {},
                OneOption: [{
                    ID: "",
                    Name: "--请选择--"
                }]
            });

            @*var personRole = "@ViewBag.personRole";//获取登录人角色-20160302
            if (personRole == "Doctor" || personRole == "Nurse") {
                Doctor = 1;//登录人为医生
            }*@

            var urlstr2 = '@Url.Content("~/IM/AllDictionary/GetMaterialTypeByUserInfo?PRole=")' + "@ViewBag.personRole";
            $.EUIcomboboxTree("#MaterialType", {
                url: urlstr2,//ajax后台取数据路径，返回的是json格式的数据
                OneOption: [{
                    id: "",
                    text: "--请选择--",
                }],
                datainfo: {},
                onChange: function (n,o) {
                    var ID;
                    if (Doctor=="1") {
                        ID = "MaterialType-15";//贵重药品
                    }
                    else {
                        if (n == "") {
                            $("#MaterialID").val("");
                            $("#MaterialTypeInfo").val("");
                            return;
                        }
                        else {
                            ID = n;
                        }
                    }
                    var urlstr3 = '@Url.Content("~/IM/AllDictionary/GetMaterial?mType=")' + ID;
                    $("#MaterialTypeInfo").val(ID);
                    $("#Material").combogrid({
                        prompt: '--请选择--',
                        panelWidth: 320,
                        idField: 'ID',
                        textField: 'Name',
                        groupField: 'QRCode',
                        columns: [[
                            { field: 'MCode', title: '代码', width: 50, align: 'right' },
                            { field: 'Name', title: '名称', width: 100 },
                            { field: 'UnitName', title: '单位', width: 50 },
                            { field: 'Specification', title: '规格', width: 50, align: 'right' },
                            { field: 'RealPrice', title: '售价', width: 50 },
                            { field: 'Unit', title: '单位', width: 50, sortable: false, hidden: true }
                        ]],
                        fitColumns: true,
                        filter: function (inputText, row) {
                            var opts = $(this).combogrid('options');
                            if (row[opts.groupField] == inputText) {
                                return true;
                            }
                            else if (row[opts.textField] == inputText) {
                                return true;
                            }
                            //else {    //2020-03-06 plq暂时注释
                            //    if (boolSearch) {
                            //        boolSearch = false;
                            //        searchBindMeterialbyName(urlstr3, inputText);
                            //    }
                            //}
                        },
                        onChange: function (n, o) {
                            //if (n == "") {
                            //    searchBindMeterialbyName(urlstr3, n);
                            //    $("#MaterialID").val("");
                            //}
                            //console.log(n);

                            //edit 2020-03-06 plq
                            if (!n) { //当新值为""或undefined时
                                filterMeterialListbyName(urlstr3, n);
                                $("#MaterialID").val("");
                            }
                            else if (boolSearch) {
                                boolSearch = false;
                                filterMeterialListbyName(urlstr3, n);   //add 2020-03-06 plq
                            }
                        },
                        onSelect: function (rec) {
                            var g = $("#Material").combogrid("grid");	// 获取数据表格对象
                            var r = g.datagrid('getSelected');	// 获取选择的行
                            $("#MaterialID").val($('#Material').combobox('getValue'));

                        },
                        //add 2020-03-06 plq
                        onHidePanel: function () { //验证输入的值必须为选项框中的数据，否则还原为空
                            var _options = $(this).combogrid('options');
                            //console.log(_options);
                            var _data = $(this).combogrid('grid').datagrid('getData');/* 下拉框所有选项 */
                            //console.log(_data);
                            var _value = $(this).combogrid('getText');/* 用户输入的值 */
                            //console.log(_value);
                            var _b = false;/* 标识是否在下拉列表中找到了用户输入的字符 */
                            for (var i = 0; i < _data.rows.length; i++) {
                                //console.log(_data.rows[i][_options.textField]);
                                if (_data.rows[i][_options.textField] == _value) {
                                    _b = true;
                                    break;
                                }
                            }
                            if (!_b) {
                                $(this).combogrid('clear'); //清空
                                $("#MaterialID").val(""); //隐藏输入——物资ID也置为空
                            }
                        }

                    });
                    searchBindMeterialbyName(urlstr3, "");
                }
            });

        }
        function searchBindMeterialbyName(urinfo, keyWord) {
            $.ajax({
                type: 'POST',
                url: urinfo,
                dataType: "json",
                success: function (jsData) {
                    //console.log(jsData);
                    //if (keyWord == "") {
                    if (!keyWord) { //当keyWord为""或undefined时 edit 2020-03-06 plq
                        $("#Material").combogrid("grid").datagrid("loadData", jsData);
                    } else {
                        for (var i = 0; i < jsData.length; i++) {
                            if (jsData[i].Name.indexOf(keyWord) < 0) {
                                if (jsData[i].MCode.indexOf(keyWord) < 0) {
                                    if (jsData[i].PinYin.indexOf(keyWord) < 0) {
                                        jsData.remove(jsData[i]);
                                        i--;
                                    }
                                }
                            }
                        }
                        $("#Material").combogrid("grid").datagrid("loadData", jsData);
                    }
                    boolSearch = true;
                }
            });
        }

        //add2020-03-06 plq---物资名称模糊查询,名称拼音开始部分匹配
        function filterMeterialListbyName(urinfo, keyWord) {
            //console.log(keyWord);
            $.ajax({
                type: 'POST',
                url: urinfo,
                dataType: "json",
                success: function (jsData) {
                    //if (keyWord == "") {
                    if (!keyWord) { //当keyWord为""或undefined时
                        $("#Material").combogrid("grid").datagrid("loadData", jsData);
                    } else {
                        for (var i = 0; i < jsData.length; i++) {
                            //console.log(pinyin.getCamelChars(jsData[i].Name));
                            if (jsData[i].Name.indexOf(keyWord) < 0) {  //如果该物资的Name不包含输入
                                //if (jsData[i].MCode.indexOf(keyWord) < 0) { //且该物资的MCode不包含输入
                                //if (jsData[i].PinYin.indexOf(keyWord) < 0) { //且物资的PinYin(每个汉字首字母小写)也不包含输入
                                //如果该物资的Name的拼音首字母缩写中也不包含输入
                                if (pinyin.getCamelChars(jsData[i].Name).toLowerCase().indexOf(pinyin.getFullChars(keyWord).toLowerCase()) < 0) {
                                    //如果该物资的Name的拼音全拼（小写）也不包含输入的(小写)(<0不包含,==0首部检索,>-1或>=0 模糊匹配)
                                    if (pinyin.getFullChars(jsData[i].Name).toLowerCase().indexOf(pinyin.getFullChars(keyWord).toLowerCase()) != 0) {
                                        jsData.remove(jsData[i]);
                                        i--;
                                    }
                                }
                                //}
                                //}
                            }
                        }
                        //console.log(jsData);
                        $("#Material").combogrid("grid").datagrid("loadData", jsData);
                    }
                    boolSearch = true;
                }
            });
        }
    </script>
}
<input type="hidden" id="userName" value="@ViewBag.userName" />
<input type="hidden" id="userID" value="@ViewBag.userID" />
<div id="TableForm" class="easyui-layout" style="width:auto; height: auto; margin-left: auto; margin-right: auto">
    <div data-options="region:'center',title:'出库物资清单',iconCls:'icon-custom-cApply'">
        <div id="SearchBar" data-options="region:'north'" style="overflow: hidden; padding: 10px">
            <table>
                <tr>
                    <td>仓库：</td>
                    <td>
                        <input id="Storage" class="easyui-combotree" data-options="required:true" style="width:160px" />
                    </td>
                    <td>分类：</td>
                    <td>
                        <input id="MaterialType" class="easyui-combobox" style="width:160px" />
                        <input id="MaterialTypeInfo" type="hidden" />
                    </td>
                    <td>物资：</td>
                    <td>
                        <input id="Material" class="easyui-combogrid" style="width:160px" />
                        <input id="MaterialID" type="hidden" />
                    </td>
                    <td>
                        &nbsp;&nbsp;
                        <a href="javascript:void(0)" id="btnsearch" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="reSearch()">查询</a>
                    </td>
                </tr>
            </table>
        </div>
        <div data-options="region:'center'" style="background: #fafafa; overflow: hidden">
            <table id="gridSurplusList"></table>
        </div>
    </div>
</div>
@Html.Partial("~/Areas/IM/Views/I_Apply/_ApplyDetail.cshtml")